package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.common.dto.MsgListDto;
import com.jilian.mydemo.common.dto.PerformDto;
import com.jilian.mydemo.common.dto.PerformListDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.my.adapter.CommonPerformAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 水工业绩 公共Fragment
 */
public abstract  class CommonPerformFragment extends BaseFragment implements CustomItemClickListener {
    private RecyclerView recyclerview;
    private LinearLayoutManager linearLayoutManager;
    private CommonPerformAdapter adapter;
    private List<PerformDto> datas;
    private OrderViewModel viewModel;
    private Integer pageNum = 1;//
    private Integer pageSize = 20;//
    private SmartRefreshLayout srHasData;
    private SmartRefreshLayout srNoData;

    @Override
    protected void loadData() {
        showLoadingDialog();
        pageNum = 1;
        performance();
    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_commonperform;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        srHasData = (SmartRefreshLayout) view.findViewById(R.id.sr_has_data);
        srNoData = (SmartRefreshLayout) view.findViewById(R.id.sr_no_data);
        srNoData.setEnableLoadMore(false);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        adapter  = new CommonPerformAdapter(getActivity(),datas,this);
        recyclerview.setAdapter(adapter);

    }

    @Override
    protected void initData() {


    }

    private void performance() {
        viewModel.performance(pageNum, pageSize, getType());
        viewModel.getPerformanceliveData().observe(this, new Observer<BaseDto<PerformListDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<PerformListDto> listBaseDto) {
                hideLoadingDialog();
                srNoData.finishRefresh();
                srHasData.finishRefresh();
                srHasData.finishLoadMore();
                //有数据
                if (listBaseDto.isSuccess()&&EmptyUtils.isNotEmpty(listBaseDto.getData())&&EmptyUtils.isNotEmpty(listBaseDto.getData().getList())) {
                    srNoData.setVisibility(View.GONE);
                    srHasData.setVisibility(View.VISIBLE);
                    if (pageNum == 1) {
                        datas.clear();
                    }
                    datas.addAll(listBaseDto.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    //说明是上拉加载
                    if (pageNum > 1) {
                        pageNum--;
                    } else {
                        //第一次就没数据
                        srNoData.setVisibility(View.VISIBLE);
                        srHasData.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    protected abstract Integer getType();

    @Override
    protected void initListener() {
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                performance();
            }
        });
        srHasData.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                performance();
            }
        });
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                performance();
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
    Intent intent = new Intent(getActivity(),HydraulicPerformanceDetailActivity.class);

    intent.putExtra("data",datas.get(position));

    startActivity(intent);
    }
}
